import { areaList } from '@vant/area-data';
const Multipart = require('../../utils/Multipart.min') 
Page({
  data: {
    areaList,
    fileList:[],
    ageList: ['1个月','1-3个月','3-6个月','6-9个月','9-12个月','1岁','2岁','3岁','3岁+'],
    isShow:false,
    active:"0",
    showPosition:false,
    info:{
      userId:"",
      patName:'',
      type:0,
      var:'',
      gender:0,
      age:'',
      jueyu:false,
      mianyi:false,
      quchong:false,
      city:'',
      number:'',
      wxCode:'',
      desc:''
    }
  },
  afterRead(e:any){
    const { file } = e.detail
    const fileList = this.data.fileList as any
    file.forEach((item:{url:string})=>{
      fileList.push(item)
    })
   this.setData({
      fileList
   })
  },
  onClose(){
    this.setData({
      isShow:false
    })
  },
  onCancel(){
    this.setData({
      isShow:false
    })
  },
  onConfirm(e:any){
    console.log(e.detail.value); 
    this.setData({
      isShow:false
    })
  },
  selectAge(){
    this.setData({
      isShow:true
    })
  },
  typeChange(e:any){
    this.setData({
      'info.type':e.detail
    })
  },
  genderChange(e:any){
    this.setData({
      'info.gender':e.detail
    })
  },
  ageConfirm(e:any){
   this.setData({
    'info.age':e.detail.value,
    isShow:false
   })
  },
  checked1(){
    let jueyu = !this.data.info.jueyu
    this.setData({
      'info.jueyu':jueyu
    })
  },
  checked2(){
    let mianyi = !this.data.info.mianyi
    this.setData({
      'info.mianyi':mianyi
    })
  },
  checked3(){
    let quchong = !this.data.info.quchong
    this.setData({
     'info.quchong':quchong
    })
  },
  selectPosition(){
    this.setData({
      showPosition:true
    })
  },
  positionClose(){
    this.setData({
      showPosition:false
    })
  },
  positionConfirm(e:any){
    let position = ""
    e.detail.values.forEach((item:any)=>{
      position = position + ' ' + item.name
    })
    this.setData({
      'info.city' : position,
      showPosition:false
    })
  },
  positionCancel(){
    this.setData({
      showPosition:false
    })
  },
  infoChange(e:any){
    let name = e.currentTarget.dataset.name
    if(name=="patName"){
      this.setData({
        "info.patName":e.detail
      })      
    }
    if(name=="var"){
      this.setData({
        "info.var":e.detail
      })
    }
    if(name=="number"){
      this.setData({
        "info.number":e.detail
      })
    }
    if(name=="wxCode"){
      this.setData({
        "info.wxCode":e.detail
      })
    }
  },
  descInfo(e:any){
    this.setData({
      "info.desc":e.detail.value
    })
  },
  async upload(){
     //获取文件
    const fileList = this.data.fileList
    let files = fileList.map((item:any)=>({name:'photo',filePath:item.url}))
    let info = this.data.info  as any
    info.jueyu = info.jueyu?1:0
    info.quchong = info.quchong?1:0
    info.mianyi = info.mianyi?1:0
    let isNotEmpt = Object.keys(info).every(k =>{
        return String(info[k]) !=""
    }) 
    if(isNotEmpt){
      let fields = Object.keys(info).map(key => ({
        name: key,
        value: info[key]
      }))
      try {
        let result = await new Multipart({files, fields})
        .submit('http://localhost:9000/v1/song')
        wx.showToast({
          title:"上传成功",
          icon:"success"
        })
        wx.switchTab({
          url:"/pages/adopt/adopt"
        })
      } catch (error) {
        wx.showToast({
          title:"上传失败",
          icon:"error" as any
        })
      }
    }else{
      wx.showToast({
        title:"信息不完整",
        icon:"error" as any
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const {id} = wx.getStorageSync("userInfo")
    this.setData({
      "info.userId":id
    })
  }
})